package com.us.usws.site.ws.entity.indexes;

import java.util.ArrayList;
import java.util.List;

import com.us.usws.biz.WSResourceURIReplacer;
import com.us.usws.common.SiteConstants;
import com.us.usws.model.domain.box.BoxTheme;
import com.us.usws.model.domain.channel.Channel;
import com.us.usws.model.domain.video.Video;
import com.us.usws.site.ws.entity.WSEntity;
import com.us.usws.site.ws.entity.WSEntityFactory;
import com.us.usws.site.ws.entity.WSEntityRecords;
import com.us.usws.site.ws.entity.indexes.index.WSIndexChannel;
import com.us.usws.site.ws.entity.indexes.index.WSIndexChannelVideo;
import com.us.usws.site.ws.entity.indexes.mychannel.WSMyChannel;
import com.us.usws.site.ws.entity.indexes.themes.WSIndexesTheme;

public class WSIndexesEntityPackGenerator {

	//private static Log log = LogFactory.getFactory().getInstance(WSEntityPackGenerator.class);

	private static WSIndexesEntityPackGenerator instance = new WSIndexesEntityPackGenerator();

	//private BeanConverter bc = BeanConverter.getDefaultConverter();

	private WSIndexesEntityPackGenerator() {
	}

	public static WSIndexesEntityPackGenerator getInstance() {
		return instance;
	}

	/*---------------- public methods -----------------*/

	/**
	 * pack for index channel
	 * @param channels
	 * @param request
	 * @return
	 */
	public WSEntity getIndexChannelPack(List<Channel> channels) {
		if (channels == null) {
			return WSEntityFactory.getEmptyEntityWithRecords();
		}
		//prepare records
		List<WSIndexChannel> records = new ArrayList<WSIndexChannel>(channels.size());

		//loop for channels
		for (Channel eachCh : channels) {

			//gen WS entity
			WSIndexChannel oneCh = new WSIndexChannel(eachCh, eachCh.getVideos());

			//set non-auto fields
			oneCh.setPicture(WSResourceURIReplacer.getChannelPictureURL(eachCh.getUid(), eachCh.getPicture()));
			oneCh.setUrl(WSResourceURIReplacer.getMyChannelURL(eachCh.getUid()));

			if (eachCh.getVideos() != null) {
				List<WSIndexChannelVideo> items = new ArrayList<WSIndexChannelVideo>(eachCh.getVideos().size());

				for (Video eachV : eachCh.getVideos()) {
					WSIndexChannelVideo oneV = new WSIndexChannelVideo(eachV);

					//set ws video props
					oneV.setVideoInfoUrl(WSResourceURIReplacer.getVideoInfoURL(eachV.getUid()));
					oneV.setSnapshot(WSResourceURIReplacer.getVideoSnapshotURL(eachV.getVideoFile().getUid(), eachV.getVideoFile()
							.getSnapshot()));

					//add  items
					items.add(oneV);
				}
				//set WsVideos to WSchannel

				oneCh.setItems(items);
			}

			//add one channel to data
			records.add(oneCh);
		}

		//pack records
		WSEntityRecords data = new WSEntityRecords();
		data.setRecords(records);

		WSEntity ret = WSEntityFactory.getDefaultWSEntity(data);
		return ret;
	}

	/**
	 * get my channel pack
	 * @param channels
	 * @return
	 */
	public WSEntity getMyChannelPack(List<Channel> channels, String focusChannelUid) {
		if (channels == null) {
			return WSEntityFactory.getEmptyEntityWithRecords();
		}
		//prepare records
		List<WSMyChannel> records = new ArrayList<WSMyChannel>(channels.size());

		//loop for channels
		for (Channel eachCh : channels) {

			//gen WS entity
			WSMyChannel oneCh = new WSMyChannel(eachCh);

			//set non-auto fields
			oneCh.setPicture(WSResourceURIReplacer.getChannelPictureURL(eachCh.getUid(), eachCh.getPicture()));
			if (eachCh.getChannelTypeDsid() == SiteConstants.DSID_CHANNEL_TYPE_MEETING) {
				oneCh.setUrl(WSResourceURIReplacer.getChannelMeetingsURL(eachCh.getUid()));
			} else {
				oneCh.setUrl(WSResourceURIReplacer.getChannelVideosListURL(eachCh.getUid()));
			}
			if (focusChannelUid == null || focusChannelUid.length() == 0) {
				focusChannelUid = eachCh.getUid();
			}
			oneCh.setSubChannelsUrl(WSResourceURIReplacer.getSubChannelsURL(eachCh.getUid(), focusChannelUid));

			//add itmes
			records.add(oneCh);
		}

		//pack records
		WSEntityRecords data = new WSEntityRecords();
		data.setRecords(records);

		WSEntity ret = WSEntityFactory.getDefaultWSEntity(data);
		return ret;
	}

	public WSEntity getThemes(List<BoxTheme> themes, boolean isIndexPage) {
		if (themes == null) {
			return WSEntityFactory.getEmptyEntityWithRecords();
		}

		List<WSIndexesTheme> records = new ArrayList<WSIndexesTheme>(themes.size());
		//loop for channels
		for (BoxTheme eachCh : themes) {
			WSIndexesTheme oneCh = new WSIndexesTheme(eachCh, isIndexPage);
			String themeUrl = WSResourceURIReplacer.getThemeInfoURL(eachCh.getUid());
			oneCh.setThemeUrl(themeUrl);
			//add one channel to data
			records.add(oneCh);
		}

		//pack records
		WSEntityRecords data = new WSEntityRecords();
		data.setRecords(records);

		WSEntity ret = WSEntityFactory.getDefaultWSEntity(data);
		return ret;
	}
}
